package org.ainlolcat.ainscience.statistics.handlers;

import org.ainlolcat.ainscience.UI.renderer.LineDataRenderer;
import org.ainlolcat.ainscience.UI.renderer.LineRendererSource;
import org.ainlolcat.ainscience.column.Column;
import org.ainlolcat.ainscience.entity.Entity;
import org.ainlolcat.ainscience.statistics.StatisticHandler;
import org.ainlolcat.ainscience.statistics.StatisticStorage;
import org.ainlolcat.ainscience.utils.ArraysUtil;
import org.ainlolcat.ainscience.utils.XMLUtils;
import org.jdom.Element;

import javax.swing.*;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;

/**
 * @author ainlolcat
 * Date: 11/5/11
 */
public class AmpStatisticHandler implements StatisticHandler , LineRendererSource{
    Hashtable<String, double[][]> data = new Hashtable<String, double[][]>();

    public JPanel getRenderer() {
        return new LineDataRenderer(this);
    }

    public Hashtable<String,String> getStatistic(Entity entity, Integer columnID) {
        throw new IllegalArgumentException();
    }

    public Hashtable<String, String> getStatistic(Entity entity, Integer columnID, Element params) {
        Hashtable<String,String> stats = new Hashtable<String, String>();
        Column cy = entity.getColumnByOrder(columnID);
        if (cy.isNumeric()){
            Column cx = entity.getXColumn(cy.getAxis());
            if (cx.isNumeric()){
                double[] x = cx.getDoubleRepresentation();
                double[] y = cy.getDoubleRepresentation();
                if (params == null || params.getChildren("range").isEmpty()) {
                    double start = x[0];
                    double end =  x[x.length-1];
                    String name = "SQD " + start + " " + end;
                    Double answer = integrate(
                            x,
                            y,
                            name);
                    StatisticStorage.getInstance().registerHandler(entity,name,this);
                    stats.put(name, answer.toString());
                }
                else {
                    for (Element integral:(List<Element>)params.getChildren("range")){
                        //try retrieve settings from params
                        double start = XMLUtils.getSafeDouble4Attribute(integral, "start", x[0]);
                        double end = XMLUtils.getSafeDouble4Attribute(integral, "end", x[x.length-1]);
                        String name = XMLUtils.getSafeString4Attribute(integral, "name", "SQD " + start + " " + end);
                        double[][] cutted = ArraysUtil.cut(x, start, end, y);

                        Double answer = integrate(
                                cutted[0],
                                cutted[1],
                                name);
                        StatisticStorage.getInstance().registerHandler(entity,name,this);
                        stats.put(name, answer.toString());
                    }
                }
            }
        }
        return stats;
    }

    private double integrate(double[] x, double[] y, String name){
        double max = 0;
        for (int i = 0 ; i < x.length ; i++){
            if (y[i] > max)
                max = y[i];
        }
        return max;
    }

    public Collection<String> getNames() {
        return data.keySet();
    }

    public double[][] getData(String name) {
        return data.get(name);
    }
}
